package com.crr.servlet;

import com.alibaba.fastjson.JSON;
import com.crr.mapper.AdminMapper;
import com.crr.mapper.EmpMapper;
import com.crr.pojo.Admin;
import com.crr.pojo.Dept;
import com.crr.pojo.Emp;
import com.crr.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
@WebServlet("/condition")
public class ConditionServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        SqlSession sqlSession= SqlSessionFactoryUtil.getSqlSession();
        EmpMapper empMapper =sqlSession.getMapper(EmpMapper.class);

        String username=req.getParameter("username");
        String password=req.getParameter("password");
        EmpMapper em = sqlSession.getMapper(EmpMapper.class);
        //将前台获取的String类型数据，转化为int类型
        // 方式1
        int sal = Integer.parseInt(req.getParameter("sal"));
        // 方式2
//        int age2 = Integer.valueOf(req.getParameter("age")).intValue();
        int deptno =Integer.parseInt(req.getParameter("deptno"));
        req.getSession().setAttribute("sal",sal);
        req.getSession().setAttribute("deptno",deptno);
        Emp emp = new Emp();
        emp.setSal(sal);
        Dept dept = new Dept();
        dept.setDeptno(deptno);
        emp.setDept(dept);
        List<Emp> list = em.getEmpBySalAndDept(emp);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        if (emp!= null) {
            out.println("<script> alert('查询成功'); location.href='show.html'</script>");

        } else {
            out.println("<script> alert('查询失败'); location.href='condition.html'</script>");
        }

}

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
